/*jslint vars: true, forin: true, sloppy: true */
var angular = require('angular');
var probe = require('../global');
probe.controller("ObjectEditor", function ($scope, $filter, ocDI,
                                           ocDialog, objectStore) {
	var list = [{name: 'loading', cssPath:'加载中'}];
	$scope.query = '';//search text
	$scope.object = {};
	$scope.refresh = false;
	$scope.doRefresh = function () {
		$scope.refresh = !$scope.refresh;
	};

	//元件库列表控制字段
	$scope.objectList = {
		//header --> editor.html
		selected: -1,
		visitFn: function () {
			ocDialog.getNewDI({
				name:'object/update',
				data:{
					objectList: $scope.object.list,
					selected: $scope.objectList.selected,
					edit: true,
					refresh: $scope.doRefresh
				}
			})
			.then(function (AddKeyDI) {
				AddKeyDI.openDialogFrom(ocDI);
			});
		}
	};
	//元件数据存放的地方

	$scope.search = function () {
		$scope.object.list = $filter('filter')(list, $scope.query);
	};
	$scope.clearSearch = function () {
		$scope.object.list = list;
	};

	$scope.clearSearch();

	$scope.deleteObject = function () {
		$scope.object.list.splice($scope.objectList.selected, 1);
	};
	$scope.trashAllObject = {
		title: '警告 - 全部清空',
		message: '您确定要清空整个公共元件库吗？',
		okFn: function () {
			$scope.object.list.length = 0;
		}
	};

	$scope.save = function () {
		objectStore.setObject(list).save().success(function () {
			ocDI.closeDialog();
		}).error(function (err) {
			console.error(err);
		});
	};

	$scope.object.list = list = objectStore.getList();
});
